import requests
import json

token = requests.session()

url = "http://localhost:5000/check"
locations = [ "BJ", "TJ", "SH", "NJ", "WH", "ZZ", "CS", "XM", "GZ", "SZ"]
data_min_0 = None#无中转
price_min_0 = 10000
data_min_1_1 = None#1次第一躺
price_min_1_1 = 10000
data_min_1_2 = None#1次第二趟
price_min_1_2 = 10000

data_min_2_1 = None#2次第一躺
price_min_2_1 = 10000
data_min_2_2 = None#2次第二趟
price_min_2_2 = 10000
data_min_2_3 = None#2次第三躺
price_min_2_3 = 10000
#无中转
for t in range(1,4):
    body = {
        "start": "SZ",
        "end": "BJ",
        "time": t,
    }
    res = token.get(url = url,params = body)
    http = res.status_code#响应状态码
    res_body = res.text#响应信息
    json_data = json.loads(res_body)#数据转换为字典
    for item in json_data:
        code = item.get("code")  # 取值code、msg等
        if http == 200 and code == 100:
            start = item.get("start")
            end = item.get("end")
            time = item.get("time")
            available = item.get("available")
            price = item.get("price")
            if price < price_min_0:
                price_min_0 = price
                data_min_0 = item
#中转一次1
for t in range(1,4):
    for end_location in locations:  # 遍历所有地点
        if end_location == "SZ" or end_location == "BJ":  # 排除起始点"SZ"
            continue
        body = {
            "start": "SZ",
            "end": end_location,  # 使用遍历中的地点作为终点
            "time": t,
        }
        res = token.get(url = url,params = body)
        http = res.status_code#响应状态码
        res_body = res.text#响应信息
        json_data = json.loads(res_body)#数据转换为字典
        for item in json_data:
            code = item.get("code")  # 取值code、msg等
            if http == 200 and code == 100:
                start = item.get("start")
                end = item.get("end")
                time = item.get("time")
                available = item.get("available")
                price = item.get("price")
                if price is not None and price < price_min_1_1 and time != 3:
                    price_min_1_1 = price
                    data_min_1_1 = item
#中转一次2
for t in range(1,4):
        body = {
            "start": data_min_1_1.get("end"),
            "end": "BJ",  # 使用遍历中的地点作为终点
            "time": t,
        }
        res = token.get(url = url,params = body)
        http = res.status_code#响应状态码
        res_body = res.text#响应信息
        json_data = json.loads(res_body)#数据转换为字典
        for item in json_data:
            code = item.get("code")  # 取值code、msg等
            if http == 200 and code == 100:
                start = item.get("start")
                end = item.get("end")
                time = item.get("time")
                available = item.get("available")
                price = item.get("price")
                if price is not None and price < price_min_1_2 and time != 1 and time != data_min_1_1.get("time"):
                    price_min_1_2 = price
                    data_min_1_2 = item

#中转2次1
for end_location in locations:  # 遍历所有地点
        if end_location == "SZ" or end_location == "BJ":  # 排除起始点"SZ"
            continue
        body = {
            "start": "SZ",
            "end": end_location,  # 使用遍历中的地点作为终点
            "time": 1,
        }
        res = token.get(url = url,params = body)
        http = res.status_code#响应状态码
        res_body = res.text#响应信息
        json_data = json.loads(res_body)#数据转换为字典
        for item in json_data:
            code = item.get("code")  # 取值code、msg等
            if http == 200 and code == 100:
                start = item.get("start")
                end = item.get("end")
                time = item.get("time")
                available = item.get("available")
                price = item.get("price")
                if price is not None and price < price_min_2_1:
                    price_min_2_1 = price
                    data_min_2_1 = item
#中转2次2
for end_location in locations:  # 遍历所有地点
        if end_location == data_min_2_1.get("end") or end_location == "SZ" or end_location == "BJ":  # 排除起始点"SZ"
            continue
        body = {
            "start": data_min_2_1.get("end"),
            "end": end_location,  # 使用遍历中的地点作为终点
            "time": 2,
        }
        res = token.get(url = url,params = body)
        http = res.status_code#响应状态码
        res_body = res.text#响应信息
        json_data = json.loads(res_body)#数据转换为字典
        for item in json_data:
            code = item.get("code")  # 取值code、msg等
            if http == 200 and code == 100:
                start = item.get("start")
                end = item.get("end")
                time = item.get("time")
                available = item.get("available")
                price = item.get("price")
                if price is not None and price < price_min_2_2:
                    price_min_2_2 = price
                    data_min_2_2 = item
#中转2次3
for end_location in locations:  # 遍历所有地点
        if end_location == data_min_2_2.get("end") or end_location == "SZ" or end_location == "BJ":  # 排除起始点"SZ"
            continue
        body = {
            "start": data_min_2_2.get("end"),
            "end": "BJ",  # 使用遍历中的地点作为终点
            "time": 3,
        }
        res = token.get(url = url,params = body)
        http = res.status_code#响应状态码
        res_body = res.text#响应信息
        json_data = json.loads(res_body)#数据转换为字典
        for item in json_data:
            code = item.get("code")  # 取值code、msg等
            if http == 200 and code == 100:
                start = item.get("start")
                end = item.get("end")
                time = item.get("time")
                available = item.get("available")
                price = item.get("price")
                if price is not None and price < price_min_2_3:
                    price_min_2_3 = price
                    data_min_2_3 = item

if data_min_0['price'] > data_min_1_1['price']+data_min_1_2['price']:
    if data_min_1_1['price']+data_min_1_2['price'] > data_min_2_1['price']+data_min_2_2['price']+data_min_2_3['price']:
        print(f"中转2次最便宜的航班信息："
              f"{data_min_2_1['start']}->"
              f"{data_min_2_2['start']}->"
              f"{data_min_2_3['start']}->"
              f"{data_min_2_3['end']},"
              f"价格为{data_min_2_1['price']+data_min_2_2['price']+data_min_2_3['price']}")
    else:
        print(f"中转1次最便宜的航班信息："
              f"{data_min_1_1['start']}->"
              f"{data_min_1_2['start']}->"
              f"{data_min_1_2['end']},"
              f"价格为{data_min_1_1['price']+data_min_1_2['price']}")
else:
    print(f"直达最便宜的航班信息:"
          f"{data_min_0['start']}->"
          f"{data_min_0['end']},"
          f"价格为{data_min_0['price']}")


















































